package thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: 582895699@qq.com
 * @Desctription: ReentrantLock
 * @Date: Created in 2021/12/10 23:28
 */
@Slf4j
public class ReentrantLockTest {
    volatile int a = 0;

    public static ReentrantLock lock = new ReentrantLock(true);

    public void add() {
        lock.lock();
        try {
            Thread thread = Thread.currentThread();
            System.out.println("【" + thread.getName() + "】获取了锁");
            a++;
            System.out.println("【" + thread.getName() + "】   【" + a + "】");
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("【" + Thread.currentThread().getName() + "】释放了锁");
            System.out.println();
        }
    }


    public static void main(String[] args) {
        new Thread(() -> {
            new ReentrantLockTest().add();
        }).start();

        new Thread(() -> {
            new ReentrantLockTest().add();
        }).start();

        new Thread(() -> {
            new ReentrantLockTest().add();
        }).start();
    }

}

